<?php

namespace app\erp\model;

use think\Model;

class OrderLogdb extends Model {
	public $param = array ();
	protected $name = 'order_log';
	protected $pk = 'order_log_id';
	protected $type = [ 
			'create_time' => 'timestamp:Y-m-d H:i' 
	];
	public function setPostJsonAttr($value) {
		return json_encode ( $value );
	}
	// 自动完成时间转换
	protected $autoWriteTimestamp = true;
	public function scopeOrderNo($query, $order_no) {
		$query->where ( 'order_no', $order_no );
	}
	public function scopeTypeIndex($query,$type_index){
	    $query->where('type_index',$type_index);
    }
	// 基础查询
	public function base($query) {
		if (isset ( $this->param ['order_no'] ) && $this->param ['order_no']) {
			$query->orderNo ( $this->param ['order_no'] );
		}
		if(isset($this->param['type_index']) && $this->param['type_index']){
		    $query->typeIndex($this->param['type_index']);
        }
	}

	// 添加数据
	public function saveData() {
		$this->allowField ( true )->save ( $this->param );
		$save_data = array ();
		$orderdb = new Orderdb ();
		if (isset ( $this->param ['type_index'] )) {
			switch ($this->param ['type_index']) {
				case 1 :
					// 创建订单
					$get_order = $orderdb->useGlobalScope ( false )->where ( 'order_no', $this->param ['order_no'] )->find ()->toArray ();
					$save_data ['order_no'] = $get_order ['order_no'];
					$save_data ['out_order_no'] = $get_order ['out_order_no'];
					$save_data ['order_id'] = $get_order ['order_id'];
					break;
				case 2 :
					// 指定供应商
					$save_data ['push_supplier_time'] = time ();
					// 反补订单时间
					$orderdb->useGlobalScope ( false )->where ( 'order_no', $this->param ['order_no'] )->update ( [ 
							'push_supplier_time' => time () 
					] );
					break;
				case 3 :
					// 供应商确认定的那
					$save_data ['supplier_confirm_time'] = time ();
					break;
				case 4 :
					// 供应商发货
					$save_data ['gys_fh_time'] = time ();
					break;
				case 5 :
					// 供应商收货
					$save_data ['gys_sh_time'] = time ();
					break;
				case 6 :
					// 供应商上传发票
					$save_data ['caigou_invoice_time'] = time ();
					// 反补订单时间
					$orderdb->useGlobalScope ( false )->where ( 'order_no', $this->param ['order_no'] )->update ( [
							'caigou_invoice_time' => time ()
					] );
					break;
				case 7 :
					// 供应商通过发票审核
					$save_data ['audit_caigou_invoice_time'] = time ();
					break;
				case 8 :
					// 供应商上传合同
					$save_data ['caigou_hetong_time'] = time ();
					// 反补订单时间
					$orderdb->useGlobalScope ( false )->where ( 'order_no', $this->param ['order_no'] )->update ( [ 
							'caigou_hetong_time' => time () 
					] );
					break;
				case 9 :
					// 订单发货
					$save_data ['jc_fh_time'] = time ();
					// 反补订单时间
					$orderdb->useGlobalScope ( false )->where ( 'order_no', $this->param ['order_no'] )->update ( [ 
							'jc_fh_time' => time () 
					] );
					break;
				case 10 :
					// 订单收货
					$save_data ['jc_sh_time'] = time ();
					// 反补订单时间
					$orderdb->useGlobalScope ( false )->where ( 'order_no', $this->param ['order_no'] )->update ( [ 
							'jc_sh_time' => time () 
					] );
					break;
				case 11 :
					// 上传发票
					$save_data ['invoice_time'] = time ();
					// 反补订单时间
					$orderdb->useGlobalScope ( false )->where ( 'order_no', $this->param ['order_no'] )->update ( [ 
							'invoice_time' => time () 
					] );
					break;
				case 12 :
					$save_data ['pay_time'] = time ();
					break;
				case 13 :
					$save_data ['ghs_pay_time'] = time ();
					break;
				case 101 :
					// 驳回发票 数据回滚
					$save_data ['type_index'] = 5;
					$save_data ['caigou_invoice_time'] = 0;
					// 反补订单时间
					$orderdb->useGlobalScope ( false )->where ( 'order_no', $this->param ['order_no'] )->update ( [ 
							'caigou_invoice_time' => null 
					] );
					break;
				case 100 :
					// 订单完成
					break;
				case 99 :
					// 订单已取消
					break;
			}
			if ($this->param ['type_index'] > 0) {
				$save_data ['step'] = $this->param ['type_index'];
				$yujingdb = new Yujingdb ();
				if ($this->param ['type_index'] == 1) {
					$yujingdb->save ( $save_data );
				} else {
					// 这里要兼容之前的数据 当前方式：之前的订单不做处理 如果要做处理 把下面的注释代码取消
					$chk_yujing = $yujingdb->useGlobalScope ( false )->where ( 'order_no', $this->param ['order_no'] )->find ();
					if (! $chk_yujing) {
						/*
						$get_order = $orderdb->useGlobalScope ( false )->where ( 'order_no', $this->param ['order_no'] )->find ();
						if($get_order){
							//$chk_yujing = $chk_yujing->toArray ();
							$get_order = $get_order->toArray ();
							$save_data ['order_no'] = $get_order ['order_no'];
							$save_data ['out_order_no'] = $get_order ['out_order_no'];
							$save_data ['order_id'] = $get_order ['order_id'];
							$save_data ['create_time'] = $get_order ['create_time'];
							$yujingdb->save ( $save_data );
						}
						*/
					} else {
						$yujingdb->where ( 'order_no', $this->param ['order_no'] )->update ( $save_data );
					}
				}
			}
		}
		return 'success';
	}
	// 记录
	public function ajaxList() {
		$data = $this->order ( 'order_log_id desc' )->select ();
		if ($data->isEmpty ()) {
			return array ();
		} else {
			$data = $data->toArray ();
			foreach ( $data as $key => $val ) {
				$data [$key] = $this->handleData ( $val );
			}
			return $data;
		}
	}
	protected function handleData($val) {
		foreach ( $val as $k => $v ) {
			$val [$k] = $v;
			switch ($k) {
				case 'member_id' :
					if ($v) {
						$memberdb = new Memberdb ();
						$get_member = $memberdb->where ( 'member_id', $v )->find ();
						if($get_member) {
                            $val ['member_info'] ['username'] = $get_member->username;
                        } else {
                            $val ['member_info'] ['username'] = '无数据';
                        }
					} else {
						$val ['member_info'] ['username'] = '已删除';
					}
					break;
				default :
					break;
			}
		}
		return $val;
	}
}